<?php
/**
 * Created by PhpStorm.
 * User: henry
 * Date: 2017/2/22
 * Time: 下午5:28
 */

namespace search\models;


use business\search\models\SearchHotModel;
use business\search\models\SearchRecordModel;
use common\utils\DateTimeUtils;
use yii\base\Model;

class BaseSearchForm extends Model
{
    const FACET_CATEGORY_1_2 = "facet_category_1_2";
    const FACET_CATEGORY_3 = "facet_category_3";

    /**
     * @var int 页数
     */
    public $page;

    /**
     * @var int 每页大小
     */
    public $pageSize = 10;

    /**
     * @var string 关键词
     */
    public $keyword;
    /**
     * @var string 一级分类的拼音
     */
    public $codeKeyLevel1;
    /**
     * @var int 一级分类代码
     */
    public $codeLevel1;
    /**
     * @var int 二级分类代码
     */
    public $codeLevel2;
    /**
     * @var int 三级分类代码
     */
    public $codeLevel3;
    /**
     * @var string 查询入口，pc端|移动端/脚本（cron）
     */
    public $portal = 'pc';

    /**
     * 获取分页时的row number
     * @return mixed
     */
    protected function getRowNumber()
    {
        if (empty($this->page)) {
            $this->page = 1;
        }
        if (!isset($this->pageSize) || $this->pageSize < 1 || $this->pageSize > 50) {
            $this->pageSize = 10;
        }
        return (intval($this->page) - 1) * intval($this->pageSize);
    }

    /**
     * 获取分词结果
     * @param $keyWord
     * @param $core
     * @param $addAsterisk
     * @return array|null
     */
    public function getAnalysisWord($keyWord, $core, $addAsterisk = true)
    {
        if (empty($keyWord) || empty($core)) {
            return $keyWord;
        }
        
    }

    /**
     * 保存搜索记录
     * @param $keyword
     * @param $analysisKeyword
     * @param $resultRows
     * @param $searchModule
     * @param $source
     */
    protected function addSearchRecord($keyword, $analysisKeyword, $resultRows, $searchModule, $source)
    {
        if (empty($keyword)) {
            return;
        }
        $searchRecord = new SearchRecordModel();
        $searchRecord->userId = '';
        $searchRecord->userType = '';
        $searchRecord->keyword = $keyword;
        $searchRecord->analysisKeyword = $analysisKeyword;
        $searchRecord->resultRows = $resultRows;
        $searchRecord->searchModule = $searchModule;
        $searchRecord->source = $source;
        //$searchRecord->ipAddress = IpAddressUtil::getClientIPAddress();
        $searchRecord->searchDate = DateTimeUtils::date();
        $searchRecord->save();
        // 当搜索结果不存在 ，不记录热门搜索
        if (empty($resultRows)) {
            return;
        }
        /** @var SearchHotModel $searchResult */
        $searchResult = SearchHotModel::find()->where(["searchModule" => $searchModule, "keyword" => $keyword])->one();
        if (empty($searchResult)) {
            $searchHot = new SearchHotModel();
            $searchHot->searchModule = $searchModule;
            $searchHot->keyword = $keyword;
            $searchHot->count = 1;
            $searchHot->save();
        } else {
            $searchResult->count = intval($searchResult->count) + 1;
            $searchResult->update();
        }
    }
}